package com.yxhl.zoume.core.user.ui.fragment.passenger;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.yxhl.zoume.R;
import com.yxhl.zoume.UIConstants;
import com.yxhl.zoume.common.ui.custom.QuicklyClearEditText;
import com.yxhl.zoume.common.ui.event.UpdatePassengerEvent;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.user.presenter.passenger.UpdatePassengerPresenter;
import com.yxhl.zoume.core.user.ui.activity.UserContainerActivity;
import com.yxhl.zoume.core.user.view.passenger.UpdatePassengerView;
import com.yxhl.zoume.data.http.model.passenger.ZMPassenger;
import com.yxhl.zoume.data.http.rest.param.passenger.PassengerParam;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.di.component.usercenter.DaggerPassengerComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.di.module.usercenter.PassengerModule;
import com.yxhl.zoume.utils.LOG;
import com.yxhl.zoume.utils.PromptUtils;
import com.yxhl.zoume.utils.ZMDialog;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * 修改乘客人信息界面
 */
public class UpdatePassengerFragment extends BaseFragment implements UpdatePassengerView {

    private static final String TAG = "UpdatePassengerFrag";

    @BindView(R.id.frame_passenger_update_root)
    FrameLayout mRootLayout;

    @BindView(R.id.et_include_name)
    QuicklyClearEditText mPassengerName;

    @BindView(R.id.et_include_id_card)
    QuicklyClearEditText mPassengerCardID;

    @BindView(R.id.et_include_mobile)
    QuicklyClearEditText mPassengerMobile;

    @BindView(R.id.rg_include_ticket_group)
    RadioGroup mTicketGroup;

    @BindView(R.id.rb_include_adult)
    RadioButton mAdult;

    @BindView(R.id.rb_include_child)
    RadioButton mChild;

    //arguments
    @Inject
    UpdatePassengerPresenter mUpdatePassengerPresenter;

    private ZMPassenger mZMPassenger;
    private UserContainerActivity mUserContainerActivity;

    public UpdatePassengerFragment() {
        // Required empty public constructor
    }

    public static UpdatePassengerFragment newInstance(ZMPassenger passenger) {
        UpdatePassengerFragment fragment = new UpdatePassengerFragment();
        if (passenger != null) {
            Bundle arguments = new Bundle();
            arguments.putSerializable(UIConstants.UserContainer.UPDATE_PASSENGER, passenger);
            fragment.setArguments(arguments);
        }
        return fragment;
    }

    private void parseArguments() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mZMPassenger = (ZMPassenger) arguments.getSerializable(UIConstants.UserContainer.UPDATE_PASSENGER);
        }
    }

    public void setZMPassenger(ZMPassenger mZMPassenger) {
        this.mZMPassenger = mZMPassenger;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        parseArguments();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        LOG.e(TAG, "onCreateView");
        mUpdatePassengerPresenter.attachView(this);
        initShow();
        bindData(mZMPassenger);
        if (mActivity instanceof UserContainerActivity) {
            mUserContainerActivity = (UserContainerActivity) mActivity;
        }
        return view;
    }

    private void initShow() {
        mPassengerName.setFocusable(false);
        mPassengerName.setEnabled(false);
    }

    /**
     * 绑定数据到UI
     *
     * @param passenger
     */
    public void bindData(ZMPassenger passenger) {
        if (passenger != null) {
            mPassengerName.setText(getStringContent(passenger.getRealName()));
            mPassengerCardID.setText(getStringContent(passenger.getIdNumber()));
            mPassengerMobile.setText(getStringContent(passenger.getMobileNumber()));
            switch (passenger.getPsgerCategory()) {
                case ZMPassenger.CHILD:
                    mChild.setChecked(true);
                    break;
                default:
                    mAdult.setChecked(true);
            }
        }
    }

    /**
     * 第二次show,状态重置
     *
     * @param zmPassenger
     */
    public void setStatus(ZMPassenger zmPassenger) {
        setupToolBar();
        setZMPassenger(zmPassenger);
        bindData(zmPassenger);
    }


    /**
     * 非空校验
     *
     * @param content
     * @return
     */
    private String getStringContent(String content) {
        if (TextUtils.isEmpty(content)) {
            content = "";
        }
        return content;
    }

    /**
     * 确认修改乘客信息
     */
    public void goToConfirm() {
        boolean enableConfirm = mUserContainerActivity.validPassengerData(mRootLayout, mPassengerName, mPassengerCardID,
                mPassengerMobile, mTicketGroup, mAdult, mChild);
        if (enableConfirm) {
            ZMDialog.getLoadingDialog(mActivity);

            mUpdatePassengerPresenter.confirmUpdatePassenger(getUpdatePassengerParam());
        }
    }

    /**
     * 获取请求参数
     *
     * @return
     */
    public PassengerParam getUpdatePassengerParam() {
        PassengerParam passengerParam = new PassengerParam(1);

        String name = mPassengerName.getText().toString().trim();
        String IDCard = mPassengerCardID.getText().toString().trim();
        String mobileNumber = mPassengerMobile.getText().toString().trim();
        int radioButtonId = mTicketGroup.getCheckedRadioButtonId();

        if (mZMPassenger != null) {
            passengerParam.setId(mZMPassenger.getPassengerId());
        }
        passengerParam.setRealname(name);
        passengerParam.setIdNumber(IDCard);
        passengerParam.setMobile(mobileNumber);
        if (radioButtonId == mChild.getId()) {
            passengerParam.setContractType(ZMPassenger.CHILD);
        } else if (radioButtonId == mAdult.getId()) {
            passengerParam.setContractType(ZMPassenger.ADULT);
        } else {
            passengerParam.setContractType(ZMPassenger.ADULT);
        }

        return passengerParam;
    }

    @Override
    public void renderDataOnSuccess(ZMPassenger zmPassenger) {
        RxBus.getInstance().send(new UpdatePassengerEvent(zmPassenger));
        ZMDialog.getLoadingDialog(mActivity).dismiss();
        Toast.makeText(mActivity, getString(R.string.update_passenger_info_success), Toast.LENGTH_SHORT).show();
        mActivity.onBackPressed();
    }

    @Override
    public void renderDataOnFailure(String errorMessage) {
        PromptUtils.showShort(mActivity, errorMessage);
    }

    @Override
    public void renderDataOnError() {
        PromptUtils.showShort(mActivity, getString(R.string.update_passenger_info_error));
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUpdatePassengerPresenter.onDestroy();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_update_passenger;
    }

    @Override
    protected void setupToolBar() {
        ((UserContainerActivity) mActivity).setToolbar(
                getString(R.string.update_passenger_info),
                getString(R.string.confirm));
    }

    @Override
    protected void initializeInjector() {
        DaggerPassengerComponent.builder()
                .activityModule(new ActivityModule(getActivity()))
                .passengerModule(new PassengerModule())
                .appComponent(getAppComponent())
                .build().inject(this);
    }


}
